//
//  OneCell.m
//  JinChengYun
//
//  Created by 金橙云 on 2017/4/12.
//  Copyright © 2017年 金橙云. All rights reserved.
//

#define P_WIDTH [UIScreen mainScreen].bounds.size.width
#import "OneCell.h"
#import "KSDatePicker.h"

#import "TodoHttpManager.h"
@implementation OneCell

-(instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier allDataModel:(KeOneModel *)dataModel
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self)
    {
        
        _manager = [TodoHttpManager sharedManager];
        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(didTDManagerKeFenXiSouSuo:) name:TDManagerKeFenXiSouSuo object:nil];
        self.dataModel = dataModel;
        
        _orageLab = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width, 30)];
        _orageLab.backgroundColor = [UIColor orangeColor];
        _orageLab.textColor = [UIColor whiteColor];
        _orageLab.font = [UIFont systemFontOfSize:14];
        _orageLab.text = @"   来访客户媒体分析";
        [self addSubview:_orageLab];
        
        _starLab =[[UILabel alloc] initWithFrame:CGRectMake(0, 30, 80, 30)];
        _starLab.textColor = [UIColor blackColor];
        _starLab.font = [UIFont systemFontOfSize:12];
        _starLab.text = @"   起止日期 :";
        [self addSubview:_starLab];
        
        _oneBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        _oneBtn.titleLabel.font = [UIFont systemFontOfSize:12];
        _oneBtn.layer.borderColor = [UIColor grayColor].CGColor;
        _oneBtn.contentHorizontalAlignment = UIControlContentHorizontalAlignmentCenter
        ;
        [_oneBtn.layer setBorderWidth:0.5];
        _oneBtn.layer.cornerRadius = 5;
        _oneBtn.tag = 90;
        [_oneBtn setTitleColor:[UIColor blackColor] forState:0];
        [_oneBtn addTarget:self action:@selector(oneBtnClick:) forControlEvents:UIControlEventTouchUpInside];
        [self addSubview:_oneBtn];
        [_oneBtn mas_makeConstraints:^(MASConstraintMaker *make) {
            make.size.mas_equalTo(CGSizeMake(80, 20));
            make.top.equalTo(_orageLab.mas_bottom).offset(5);
            make.left.equalTo(_starLab.mas_right).offset(10);
            
        }];
        
        UILabel *zhiLab = [[UILabel alloc] init];
        zhiLab.text = @"至";
        zhiLab.textColor = [UIColor blackColor];
        zhiLab.font = [UIFont systemFontOfSize:12];
        zhiLab.textAlignment = NSTextAlignmentCenter;
        [self addSubview:zhiLab];
        [zhiLab mas_makeConstraints:^(MASConstraintMaker *make) {
            make.size.mas_equalTo(CGSizeMake(15, 20));
            make.top.equalTo(_orageLab.mas_bottom).offset(5);
            make.left.equalTo(_oneBtn.mas_right).offset(2);
            
        }];
        _twoBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        _twoBtn.titleLabel.font = [UIFont systemFontOfSize:12];
        _twoBtn.layer.borderColor = [UIColor grayColor].CGColor;
        _twoBtn.contentHorizontalAlignment = UIControlContentHorizontalAlignmentCenter
        ;
        [_twoBtn.layer setBorderWidth:0.5];
        _twoBtn.layer.cornerRadius = 5;
        _twoBtn.tag = 91;
        [_twoBtn setTitleColor:[UIColor blackColor] forState:0];
        [_twoBtn addTarget:self action:@selector(oneBtnClick:) forControlEvents:UIControlEventTouchUpInside];
        [self addSubview:_twoBtn];
        [_twoBtn mas_makeConstraints:^(MASConstraintMaker *make) {
            make.size.mas_equalTo(CGSizeMake(80, 20));
            make.top.equalTo(_orageLab.mas_bottom).offset(5);
            make.left.equalTo(zhiLab.mas_right).offset(2);
            
        }];
        
        _seacherBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        [_seacherBtn setBackgroundImage:[UIImage imageNamed:@"Orange_seacher"] forState:0];
        [_seacherBtn addTarget:self action:@selector(seacherBtnClick:) forControlEvents:UIControlEventTouchUpInside];
        [self addSubview:_seacherBtn];
        [_seacherBtn mas_makeConstraints:^(MASConstraintMaker *make) {
            make.size.mas_equalTo(CGSizeMake(17, 17));
            make.top.equalTo(_orageLab.mas_bottom).offset(5);
            make.left.equalTo(_twoBtn.mas_right).offset(8);
            
        }];
        UILabel *oneLine = [[UILabel alloc] init];
        oneLine.backgroundColor = [UIColor grayColor];
        [self addSubview:oneLine];
        [oneLine mas_makeConstraints:^(MASConstraintMaker *make) {
            make.size.mas_equalTo(CGSizeMake(P_WIDTH, 0.5f));
            make.top.equalTo(_orageLab.mas_bottom).offset(30);
        }];
        
        
        _visitLab = [[UILabel alloc] init];
        _visitLab.textColor = [UIColor blackColor];
        _visitLab.textAlignment = NSTextAlignmentLeft;
        _visitLab.font = [UIFont systemFontOfSize:12];
        _visitLab.text = @"来访客户 :200";
       _visitLab.text = [NSString stringWithFormat:@"来访客户:%@",self.dataModel.mtfx.visit_num];
        [self addSubview:_visitLab];
        [_visitLab mas_makeConstraints:^(MASConstraintMaker *make) {
            make.size.mas_equalTo(CGSizeMake(200, 20));
            make.left.equalTo(self.mas_left).offset(10);
            make.top.equalTo(oneLine.mas_bottom).offset(5);
        }];
        
        UILabel *twoLine = [[UILabel alloc] init];
        twoLine.backgroundColor = [UIColor orangeColor];
        [self addSubview:twoLine];
        [twoLine mas_makeConstraints:^(MASConstraintMaker *make) {
            make.size.mas_equalTo(CGSizeMake(P_WIDTH, 1.0f));
            make.top.equalTo(_orageLab.mas_bottom).offset(60);
        }];
        
        /** 图表上label字体大小(默认为10.f) */
//@property (nonatomic, strong) UIFont * valueOnChartFont;
        /** y轴(普通图表) 或 x轴(横向图表) 上名称字体大小(默认为10.f) */
        //@property (nonatomic, strong) UIFont * axisLineNameFont;
        /** y轴(普通图表) 或 x轴(横向图表) 上数值字体大小(默认为10.f) */
        //@property (nonatomic, strong) UIFont * axisLineValueFont;
        self.barChart = [[ZFBarChart alloc] initWithFrame:CGRectMake(-40, 91, SCREEN_WIDTH+40, 205)];
        self.barChart.dataSource = self;
        //self.barChart.valueOnChartFont = [UIFont systemFontOfSize:8];
//        self.barChart.axisLineNameFont = [UIFont systemFontOfSize:8];
       self.barChart.axisLineValueColor = [UIColor whiteColor];

        self.barChart.delegate = self;
        self.barChart.isResetAxisLineMaxValue = YES;
        [self addSubview:self.barChart];
        [self.barChart strokePath];
    }
    return self;
}
-(void)didTDManagerKeFenXiSouSuo:(NSNotification *)not
{
    if (not.object != 0)
    {
        NSLog(@"=====%@",not.object);
       // [self.barChart reloadInputViews];
    }
    
}
-(void)dealloc
{
    
    
    [[NSNotificationCenter defaultCenter] removeObserver:self name:TDManagerKeFenXiSouSuo object:nil];
    
}

-(void)oneBtnClick:(UIButton *)sender
{
    KSDatePicker* picker = [[KSDatePicker alloc] initWithFrame:CGRectMake(0, 0, self.frame.size.width - 40, 300)];
    picker.appearance.radius = 5;
    
    //设置回调
    picker.appearance.resultCallBack = ^void(KSDatePicker* datePicker,NSDate* currentDate,KSDatePickerButtonType buttonType){
        
        if (buttonType == KSDatePickerButtonCommit) {
            
            NSDateFormatter* formatter = [[NSDateFormatter alloc] init];
            [formatter setDateFormat:@"yyyy-MM-dd"];
            
            
            if (sender.tag == 90)
            {
                [sender setTitle:[formatter stringFromDate:currentDate] forState:UIControlStateNormal];

               oneStr =sender.titleLabel.text;
                NSLog(@"=======1111-------%@",oneStr);
            }
            else
            {
                [sender setTitle:[formatter stringFromDate:currentDate] forState:UIControlStateNormal];
              twoStr= sender.titleLabel.text;
                  NSLog(@"=======5555551-------%@",twoStr);
            }
        }
    };
    // 显示
    [picker show];
}

-(void)seacherBtnClick:(UIButton *)btn
{
    [_manager userManagerKeFenXiSouSuoWithStartTime:oneStr withEndTime:twoStr withType:@"1"];
}

#pragma mark - ZFGenericChartDataSource

- (NSArray *)valueArrayInGenericChart:(ZFGenericChart *)chart{
    
    
    NSMutableArray *dataArr = [[NSMutableArray alloc] init];
    for (FDataOneModel *fdata in self.dataModel.mtfx.con) {
        NSLog(@"第一个cell   ----===========  %@",fdata.count);
        [dataArr addObject:fdata.count];
    }
    
    NSLog(@"第一个cell   =====-----    %@",dataArr);
    NSLog(@"=======%ld",dataArr.count);
    return dataArr;
 
}



- (NSArray *)nameArrayInGenericChart:(ZFGenericChart *)chart{
    
    NSMutableArray *dataArr = [[NSMutableArray alloc] init];
    for (FDataOneModel *fdata in self.dataModel.mtfx.con) {
        [dataArr addObject:fdata.name];
    }
    
    
    //NSLog(@"第一个cell    =====-----    %@",self.dataModel.mtfx.max_data);
    
    return dataArr;
    //return @[@"一年级", @"二年级", @"三年级", @"四年级", @"五年级", @"六年级"];
}

- (NSArray *)colorArrayInGenericChart:(ZFGenericChart *)chart{
    return @[ZFLvColor];
}

- (CGFloat)axisLineMaxValueInGenericChart:(ZFGenericChart *)chart{
    if (self.dataModel.mtfx.max_data) {
        return [self.dataModel.mtfx.max_data intValue];
    }else{
        return 200;
    }
    return 200;
}

//最大值
- (NSUInteger)axisLineSectionCountInGenericChart:(ZFGenericChart *)chart{
    if (self.dataModel.mtfx.max_data) {
        return [self.dataModel.mtfx.max_data intValue];
    }else{
        return 10;
    }
    
   // return 10;
}
- (NSArray *)gradientColorArrayInBarChart:(ZFBarChart *)barChart{
    ZFGradientAttribute * gradientAttribute1 = [[ZFGradientAttribute alloc] init];
    gradientAttribute1.colors = @[(id)ZFColor(15, 212, 201, 1).CGColor, (id)ZFWhite.CGColor];
    gradientAttribute1.locations = @[@(0.5), @(0.99)];
    gradientAttribute1.startPoint = CGPointMake(0, 0.5);
    gradientAttribute1.endPoint = CGPointMake(1, 0.5);
    
    return [NSArray arrayWithObjects:gradientAttribute1, nil];
}

- (void)awakeFromNib {
    [super awakeFromNib];
    // Initialization code
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

@end
